using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Threading;

using SqlWebAdmin.CSharp;

public partial class DatabaseProperties : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        chkDataFileAutoGrowFile.Attributes.Add("onclick", "chkDataFileAutoGrowFile_onclick(this);");
        chkLogFileAutoGrowFile.Attributes.Add("onclick", "chkLogFileAutoGrowFile_onclick(this);");

        if (!IsPostBack)
        {
           
                lblError.Text = string.Empty;
                lblError.Visible = false;

                Server smoServer  = SmoHelper.GetServer();
                try
                {
                     
                    string databaseName = Request.QueryString["database"];
                    Database smoDatabase = smoServer.Databases[databaseName];

                    if (smoDatabase != null)
                    {
                        lblNameProperty.Text = HttpUtility.HtmlEncode(smoDatabase.Name);
                        lblStatusProperty.Text = HttpUtility.HtmlEncode(smoDatabase.Status.ToString());
                        lblDateCreatedProperty.Text = HttpUtility.HtmlEncode(smoDatabase.CreateDate.ToString(Thread.CurrentThread.CurrentUICulture));
                        lblNumberOfUsersProperty.Text = smoDatabase.ActiveConnections.ToString(Thread.CurrentThread.CurrentUICulture);

                        if (smoDatabase.LastBackupDate.ToString(Thread.CurrentThread.CurrentUICulture) != "1/1/0001 12:00:00 AM")
                        {
                            lblLastDataBackup.Text = smoDatabase.LastBackupDate.ToString(Thread.CurrentThread.CurrentUICulture);
                        }
                        else
                        {
                            lblLastDataBackup.Text = "Never";
                        }

                        if (smoDatabase.LastLogBackupDate.ToString(Thread.CurrentThread.CurrentUICulture) != "1/1/0001 12:00:00 AM")
                        {
                            lblLastTransactionBackup.Text = smoDatabase.LastLogBackupDate.ToString(Thread.CurrentThread.CurrentUICulture);
                        }
                        else
                        {
                            lblLastTransactionBackup.Text = "Never";
                        }

                        //lblSizeProperty.Text = (smoDatabase.Size / 1024).ToString(Thread.CurrentThread.CurrentUICulture);
                        //lblSpaceAvailableProperty.Text = (smoDatabase.SpaceAvailable / 1024).ToString(Thread.CurrentThread.CurrentUICulture);
                        //lblFullText.Text = smoDatabase.IsFullTextEnabled.ToString(Thread.CurrentThread.CurrentUICulture);

                        chartcontrol1.Border.Color = System.Drawing.Color.White;
                        chartcontrol1.Legend.Border.Color = System.Drawing.Color.White;

                        WebChart.ChartPoint spaceAvailable = new WebChart.ChartPoint();
                        WebChart.ChartPoint spaceUsed = new WebChart.ChartPoint();

                        spaceAvailable.XValue = "Space Available";
                        spaceAvailable.YValue = float.Parse((smoDatabase.SpaceAvailable / 1024).ToString(Thread.CurrentThread.CurrentUICulture)); ;
                        
                        spaceUsed.XValue = "Space Used";
                        spaceUsed.YValue = float.Parse((smoDatabase.Size / 1024).ToString(Thread.CurrentThread.CurrentUICulture));

                        chartcontrol1.Charts[0].Data.Add(spaceAvailable);
                        chartcontrol1.Charts[0].Data.Add(spaceUsed);

                        chartcontrol1.RedrawChart();

                        DataFile dataFile = smoDatabase.FileGroups[0].Files[0];
                        if (dataFile.GrowthType == FileGrowthType.KB ||
                            dataFile.GrowthType == FileGrowthType.Percent)
                        {
                            ddlDataFileGrowthType.Items.FindByText(dataFile.GrowthType.ToString()).Selected = true;
                            txtDataFileGrowth.Text = dataFile.Growth.ToString(Thread.CurrentThread.CurrentUICulture);
                        }

                        if (dataFile.MaxSize == -1.0)
                        {
                            optDataFileUnrestrictedGrowth.Checked = true;
                            txtDataFileMaxFileSize.Text = string.Empty;
                        }
                        else
                        {
                            optDataFileRestrictGrowth.Checked = true;
                            txtDataFileMaxFileSize.Text = dataFile.MaxSize.ToString(Thread.CurrentThread.CurrentUICulture);
                        }

                        if (dataFile.GrowthType.ToString() == "None")
                        {
                            chkDataFileAutoGrowFile.Checked = false;
                            ddlDataFileGrowthType.Enabled = false;
                            panDataFile.Enabled = false;
                        }
                        else
                        {
                            chkDataFileAutoGrowFile.Checked = true;
                            ddlDataFileGrowthType.Enabled = true;
                            panDataFile.Enabled = true;
                        }

                        LogFile logFile = smoDatabase.LogFiles[0];
                        if (logFile.GrowthType == FileGrowthType.KB ||
                            logFile.GrowthType == FileGrowthType.Percent)
                        {
                            ddlLogFileGrowthType.Items.FindByText(logFile.GrowthType.ToString()).Selected = true;
                            txtLogFileGrowth.Text = logFile.Growth.ToString(Thread.CurrentThread.CurrentUICulture);
                        }

                        if (logFile.MaxSize == -1.0)
                        {
                            optLogFileUnrestrictedGrowth.Checked = true;
                            txtLogFileMaxFileSize.Text = string.Empty;
                        }
                        else
                        {
                            optLogFileRestrictGrowth.Checked = true;
                            txtLogFileMaxFileSize.Text = logFile.MaxSize.ToString(Thread.CurrentThread.CurrentUICulture);
                        }

                        if (logFile.GrowthType.ToString() == "None")
                        {
                            chkLogFileAutoGrowFile.Checked = false;
                            ddlLogFileGrowthType.Enabled = false;
                            panLogFile.Enabled = false;
                        }
                        else
                        {
                            chkLogFileAutoGrowFile.Checked = true;
                            ddlLogFileGrowthType.Enabled = true;
                            panLogFile.Enabled = true;
                        }                        
                    }
                    else
                    {
                        Response.Redirect(String.Format("error.aspx?error={0}", Globals.databaseNotFoundErrorNumber), false);
                    }
                }
                catch (SmoException smoEx)
                {
                    lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(smoEx));
                    lblError.Visible = true;
                }
                catch (System.Exception ex)
                {
                    lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(ex));
                    lblError.Visible = true;
				}
				finally
				{
					smoServer.ConnectionContext.Disconnect();
				}

            }
        

    }

    protected void cmdApply_Click(object sender, EventArgs e)
    {
       
            lblError.Visible = false;

			Server smoServer = SmoHelper.GetServer();
            try
            {
                    string databaseName = Request.QueryString["database"];
                    Database smoDatabase = smoServer.Databases[databaseName];

                    if (smoDatabase != null)
                    {
                        DataFile dataFile = smoDatabase.FileGroups[0].Files[0];

                        if (chkDataFileAutoGrowFile.Checked)
                        {
                            if (txtDataFileGrowth.Text.Trim() == string.Empty)
                            {
                                txtDataFileGrowth.Text = "1024";
                            }

                            Double dataFileGrowth = Double.Parse(txtDataFileGrowth.Text.Trim());
                            if (dataFileGrowth < 0.0)
                            {
                                dataFileGrowth = 0.0;
                            }
                            dataFile.Growth = dataFileGrowth;

                            switch( (FileGrowthType)Enum.Parse(typeof(FileGrowthType), ddlDataFileGrowthType.Text ) )
                            {
                                case FileGrowthType.KB:
                                    dataFile.GrowthType = FileGrowthType.KB;
                                    break;
                                case FileGrowthType.Percent:
                                    dataFile.GrowthType = FileGrowthType.Percent;
                                    break;
                                default:
                                    dataFile.GrowthType = FileGrowthType.None;
                                    break;
                            }

                            if (optDataFileUnrestrictedGrowth.Checked)
                            {
                                dataFile.MaxSize = -1.0;
                                txtDataFileMaxFileSize.Text = string.Empty;
                            }
                            else
                            {
                                if (txtDataFileMaxFileSize.Text.Trim() == string.Empty)
                                {
                                    txtDataFileMaxFileSize.Text = "1024";
                                }
                                Double dataFileMaxFileSize = Double.Parse(txtDataFileMaxFileSize.Text.Trim());
                                dataFile.MaxSize = dataFileMaxFileSize;
                            }

                            panDataFile.Enabled = true;
                            ddlDataFileGrowthType.Enabled = true;
                        }
                        else
                        {
                            dataFile.GrowthType = 0.0;
                            dataFile.GrowthType = FileGrowthType.None;
                            dataFile.MaxSize = -1.0;

                            ddlDataFileGrowthType.Enabled = false;
                            panDataFile.Enabled = false;
                        }

                        dataFile.Alter();

                        LogFile logFile = smoDatabase.LogFiles[0];

                        if (chkLogFileAutoGrowFile.Checked)
                        {
                            if (txtLogFileGrowth.Text.Trim() == string.Empty)
                            {
                                txtLogFileGrowth.Text = "10";
                            }

                            Double logFileGrowth = Double.Parse(txtLogFileGrowth.Text.Trim());
                            if (logFileGrowth < 0.0)
                            {
                                logFileGrowth = 10.0;
                            }
                            logFile.Growth = logFileGrowth;

                            switch ((FileGrowthType)Enum.Parse(typeof(FileGrowthType), ddlLogFileGrowthType.SelectedItem.Text))
                            {
                                case FileGrowthType.KB:
                                    logFile.GrowthType = FileGrowthType.KB;
                                    break;
                                case FileGrowthType.Percent:
                                    logFile.GrowthType = FileGrowthType.Percent;
                                    break;
                                default:
                                    logFile.GrowthType = FileGrowthType.None;
                                    break;
                            }

                            if (optLogFileUnrestrictedGrowth.Checked)
                            {
                                logFile.MaxSize = -1.0;
                                txtLogFileMaxFileSize.Text = string.Empty;
                            }
                            else
                            {
                                if (txtLogFileMaxFileSize.Text.Trim() == string.Empty)
                                {
                                    txtLogFileMaxFileSize.Text = "2147483648";
                                }
                                Double logFileMaxFileSize = Double.Parse(txtLogFileMaxFileSize.Text.Trim());
                                logFile.MaxSize = logFileMaxFileSize;
                            }

                            panLogFile.Enabled = true;
                            ddlLogFileGrowthType.Enabled = true;
                        }
                        else
                        {
                            logFile.GrowthType = 0.0;
                            logFile.GrowthType = FileGrowthType.None;
                            logFile.MaxSize = -1.0;

                            ddlLogFileGrowthType.Enabled = false;
                            panLogFile.Enabled = false;
                        }

                        logFile.Alter();
                    }
                    else
                    {
                        Response.Redirect(String.Format("error.aspx?error={0}", Globals.databaseNotFoundErrorNumber), false);
                    }
            }
            catch (SmoException smoEx)
            {
                lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(smoEx));
                lblError.Visible = true;
            }
            catch (System.Exception ex)
            {
                lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(ex));
                lblError.Visible = true;
            }
        }

        
    
}
